package com.itwn.springcloud.controller;

import com.itwn.springcloud.entityes.CommonResult;
import com.itwn.springcloud.entityes.Payment;
import com.itwn.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *      生产者
 *
 */
@RestController
@Slf4j  //日志
public class PaymentController {

    @Resource   //和Autowired  差不多
    private PaymentService paymentService;

    /**
     * 负载均衡
     * 多个提供者
     * 读取    application.yml配置的server.port
     */
    @Value("${server.port}")
    private String serverPort;

    /**
     * 通过服务发现去获取服务信息
     *      要在启动类上面加@EnableDiscoveryClient
     *
     */
    @Resource
    private DiscoveryClient discoveryClient;


    @PostMapping(value="/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        System.out.println(payment);
        int result = paymentService.create(payment);
        System.out.println(result);
//        log.info("create ==> " + Result);

        if(result >0){
            return new CommonResult(200,"插入数据成功,server.port"+serverPort,result);
        }else{
            return new CommonResult(444,"插入数据失败,server.port"+serverPort,null);
        }

    }


    @GetMapping(value="/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        System.out.println("getPaymentById ==> "+id);
        Payment payment = paymentService.getPaymentById(id);
        System.out.println(payment);
//        log.info("create ==> " + Result);

        if(payment !=null){
            return new CommonResult(200,"查询成功,server.port"+serverPort,payment);
        }else{
            return new CommonResult(444,"没有对应记录,server.port"+serverPort,null);
        }

    }


    @GetMapping("/payment/discovery")
    public Object discovery(){
        //1.0获得服务列表清单   CLOUD-ORDER-SERVICE     CLOUD-PAYMENT-SERVICE
        List<String> services = discoveryClient.getServices();

        for (String service : services) {
            System.out.println(service);
        }

        //2.0 根据微服务的serviceId 获取微服务提供者的集
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");

        for (ServiceInstance instance : instances) {
            System.out.println("Host ==>"+instance.getHost());
            System.out.println("Port ==>"+instance.getPort());
            System.out.println("ServiceId ==>"+instance.getServiceId());
            System.out.println("Uri ==>"+instance.getUri());
        }

        return this.discoveryClient;
    }

    @GetMapping("/payment/lb")
    public String getPaymentLb(){
        return serverPort;
    }



    @GetMapping("/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try {
            //模拟
            //设置业务的反应时间为3秒钟,客户端请求消息发送后三秒钟发送请求的信息
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serverPort;
    }



}
